package com.wushi.lenovo.asia5b.app_di.login.presenter;

import com.mvp.base.BasePresenter;
import com.mvp.base.NetParams;
import com.mvp.base.model.ResultModel;
import com.mvp.utils.MD5Util;
import com.mvp.utils.ToastUtil;
import com.mvp.utils.https.MapHelper;
import com.wushi.lenovo.asia5b.app_di.login.mvpview.ProfileActivityView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_utils.WTimeUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;
import timber.log.Timber;


/**
 * Created by zhangyuncai on 2019/3/14.
 */
public class ProfileActivityPresenter extends BasePresenter<ProfileActivityView> {


    ApiService apiService;

    @Inject
    public ProfileActivityPresenter(ApiService apiService) {
        this.apiService = apiService;
    }

    private static final String AUTHORIZATION_HEADER_VALUE_WITH_PLACEHOLDER = "Bearer %s";

    final String privateKey = "ASIA5B_2020_7uepVrlKxWE7eahtaXHr7wtg";

    public void logout() {
        if (false) {
            getView().logout();
            return;
        }
        String token = String.format(AUTHORIZATION_HEADER_VALUE_WITH_PLACEHOLDER, UserInfo.getUserToken());
        MapHelper map = MapHelper.newHashMap();
        map.put("member_id", UserInfo.getMemberId());
        //使用吉隆坡时间(其实和北京时间是一样的)
        Locale ms = new Locale("ms");
        map.put("logout_time", WTimeUtils.getDateFormatter(new Date(), WTimeUtils.text_yyyy_MM_dd_HH_mm_ss, ms));
        long time = Calendar.getInstance(ms).getTime().getTime();
        String logout_key = String.format("%d_%s_%s", UserInfo.getMemberId(), (time / 1000) + "", privateKey);
        Timber.d("--->>>>>>>>---logout_key:" + logout_key);
        String md5LogoutKey = MD5Util.INSTANCE.MD5Encode(logout_key, null);
        Timber.d("--->>>>>>>>---md5LogoutKey:" + md5LogoutKey);
        map.put("logout_key", md5LogoutKey);
        NetParams netParams = NetParams.Companion.isToastError(false);
        baseSubscribe(apiService.logout(token, map), netParams, new Consumer<ResultModel>() {
            @Override
            public void accept(ResultModel resultModel) throws Exception {
                if (resultModel.isSuccess()) {
                    ToastUtil.showToast(resultModel.getMsg());
                }
                getView().logout();
            }
        }, throwable -> {
            getView().logout();
        });
    }
}